package com.example.demo.controller;

import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@CrossOrigin(origins = "*")
@RestController
public class ImageController {
    private final ResourceLoader resourceLoader;
    public ImageController(ResourceLoader resourceLoader) {
        this.resourceLoader = resourceLoader;
    }
    // ...
    @GetMapping("/getImages")
    public List<String> getImages() throws IOException {
        List<String> images = new ArrayList<>();
        // 获取static/source文件夹的绝对路径
        Resource resource = resourceLoader.getResource("classpath:static/swiper/");
        File sourceDir = resource.getFile();
        // 遍历source文件夹中的所有文件，找到图片文件
        File[] files = sourceDir.listFiles();
        if (files != null) {
            for (File file : files) {
                if (file.isFile()) {
                    String imageUrl = "http://localhost:8080/swiper/" + file.getName();
                    images.add(imageUrl);

                }
            }
        }

        return images;
    }
}